﻿using FBroSharp.Callback;
using FBroSharp.Lib;
using FBroSharp.Value;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace VIP高级功能测试
{
    public class VIPJSCallback : FBroSharpGeneralResultCallback
    {
        public override void CallbackData(IFBroSharpBrowser browser, int message_id, bool success,Stream data)
        {
            StreamReader reader = new StreamReader(data, Encoding.UTF8);

            Console.WriteLine(MethodBase.GetCurrentMethod().Name+":" + reader.ReadToEnd());
        }
    }

    public class VIPPageCallback: FBroSharpGeneralResultCallback
    {
        public override void CallbackData(IFBroSharpBrowser browser, int message_id, bool success, Stream data)
        {
            Console.WriteLine(MethodBase.GetCurrentMethod().Name);

            if (success)
            {
                //将数据转换成Image数据
                Image img = Image.FromStream(data);

                //这里采用线程创建窗口，避免柱塞事件导致各种不可预料的问题
                Thread thread = new Thread(() => {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new ImageForm
                    {
                        //将图片数据保存到窗口变量中
                        image_data_ = img
                    });
                });
                thread.Start();
            }
            else
            {
                //如果获取失败，这里将会是错误信息
                StreamReader reader = new StreamReader(data, Encoding.UTF8);
                Console.WriteLine(MethodBase.GetCurrentMethod().Name + ":" + reader.ReadToEnd());
            }
        }
    }

}
